<?php

namespace Plugins\AdminBasic\Http\Admin\Controller;

use App\Http\AbstractController;
use App\Middlewares\Admin\AuthenticateMiddleware;
use App\Middlewares\Admin\PermissionsMiddleware;
use App\Util\Helper;
use App\Util\Response;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\View\Render;
use Plugins\AdminBasic\Enums\BasicSuperConfigEnumType;
use Plugins\AdminBasic\Models\BasicSuperConfig;
use Psr\Http\Message\ResponseInterface;
use Plugins\AdminBasic\Http\Admin\Service\SuperConfigService;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Validation\Annotation\Scene;
use App\Exception\ApiMessageException;
use Hyperf\Validation\Middleware\ValidationMiddleware;
use Plugins\AdminBasic\Requests\BasicSuperConfigRequest;
use Psr\SimpleCache\InvalidArgumentException;


/**
 * 超级配置
 */
#[Controller(prefix: 'admin/plugins/admin-basic/super_config')]
#[Middlewares([AuthenticateMiddleware::class, PermissionsMiddleware::class])]
class SuperConfigController extends AbstractController
{


    /**
     * 列表页
     * 
     * @param Render $render
     * 
     * @return ResponseInterface
     */
    #[GetMapping(path: 'lists')]
    public function lists(Render $render): ResponseInterface
    {
        return $render->render('plugins.AdminBasic.Admin.SuperConfig.lists');
    }

    /**
     * 列表数据
     * 
     * @return ResponseInterface
     */
    #[GetMapping(path: 'lists-data')]
    public function listsData(): ResponseInterface
    {
        return Response::json()->success(SuperConfigService::aop()->lists());
    }

    /**
     * 创建数据页面
     * 
     * @param Render $render
     * 
     * @return ResponseInterface
     */
    #[GetMapping(path: 'create')]
    public function create(Render $render): ResponseInterface
    {
        return $render->render('plugins.AdminBasic.Admin.SuperConfig.edit');
    }

    /**
     * 创建数据
     * 
     * @param BasicSuperConfigRequest $request
     * 
     * @return ResponseInterface
     * @throws ApiMessageException
     * @throws InvalidArgumentException
     */
    #[Scene('store')]
    #[PostMapping(path: 'create')]
    #[Middlewares([ValidationMiddleware::class])]
    public function store(BasicSuperConfigRequest $request): ResponseInterface
    {
        SuperConfigService::aop()->store($request->post());
        return Response::json()->success();
    }

    /**
     * 更新数据页面
     * 
     * @param Render $render
     * 
     * @return ResponseInterface
     */
    #[GetMapping(path: 'update')]
    public function edit(Render $render): ResponseInterface
    {
        return $render->render('plugins.AdminBasic.Admin.SuperConfig.edit');
    }

    /**
     * 更新数据
     * 
     * @param BasicSuperConfigRequest $request
     * 
     * @return ResponseInterface
     * @throws ApiMessageException
     * @throws InvalidArgumentException
     */
    #[Scene('update')]
    #[PostMapping(path: 'update')]
    #[Middlewares([ValidationMiddleware::class])]
    public function update(BasicSuperConfigRequest $request): ResponseInterface
    {
        SuperConfigService::aop()->update($request->post());
        return Response::json()->success();
    }

    /**
     * 删除数据
     * 
     * @param BasicSuperConfigRequest $request
     * 
     * @return ResponseInterface
     * @throws ApiMessageException
     */
    #[Scene('destroy')]
    #[PostMapping(path: 'destroy')]
    #[Middlewares([ValidationMiddleware::class])]
    public function destroy(BasicSuperConfigRequest $request): ResponseInterface
    {
        SuperConfigService::aop()->destroy($request->post('ids'));
        return Response::json()->success();
    }

    /**
     * 基础配置
     *
     * @param Render $render
     *
     * @return ResponseInterface
     */
    #[GetMapping(path: 'base-config')]
    public function baseConfig(Render $render): ResponseInterface
    {
        $superConfig = BasicSuperConfig::find(Helper::request()->query('id'));

        return $render->render($superConfig->type != BasicSuperConfigEnumType::AdvancedArray
            ? 'plugins.AdminBasic.Admin.SuperConfig.config'
            : 'plugins.AdminBasic.Admin.SuperConfig.advancedArrayConfig',

            ['superConfig' => $superConfig]
        );
    }

    /**
     * 保存|更新基础配置
     *
     * @param BasicSuperConfigRequest $superConfigRequest
     * @return ResponseInterface
     * @throws ApiMessageException
     */
    #[Scene('updateBaseConfig')]
    #[PostMapping(path: 'base-config')]
    public function updateBaseConfig(BasicSuperConfigRequest $superConfigRequest): ResponseInterface
    {
        SuperConfigService::aop()->saveBase($superConfigRequest->post());

        return Response::json()->success();
    }

    /**
     * 高级数组数据
     *
     * @return ResponseInterface
     * @throws InvalidArgumentException
     */
    #[GetMapping(path: 'advanced-array-data')]
    public function advancedArrayData(): ResponseInterface
    {
        $data = SuperConfigService::aop()->advancedArrayData(
            (int)$this->request->query('page'),
            (int)$this->request->query('pageSize')
        );

        return Response::json()->success($data);
    }

    /**
     * 高级数组数据删除
     *
     * @return ResponseInterface
     * @throws ApiMessageException
     */
    #[PostMapping(path: 'advanced-array-data-destroy')]
    public function advancedArrayDataDestroy(): ResponseInterface
    {
        $id        = $this->request->post('id');
        $deleteIds = $this->request->post('ids');

        SuperConfigService::aop()->advancedArrayDataDestroy((int)$id, $deleteIds ?? []);

        return Response::json()->success();
    }
}